package xyccontroller

import (
	"Cgo/common"
	"Cgo/front/models"
	"strconv"

	"github.com/gin-gonic/gin"
)

func XycOrderController(r *gin.RouterGroup) {
	r.POST("/addOrder", common.HandlerFunc(addOrder))
	r.GET("/findOrderByUserId", common.HandlerFunc(findOrderByUserId))
}

// 添加订单
// @Summary 添加订单
// @description 添加订单
// @Tags xyc_shop前台接口
// @Accept json
// @Produce json
// @Param order body models.XycOrder true "订单信息"
// @Success 200 {object} common.RT[string]
// @Router /front/xyc/order/addOrder [post]
func addOrder(c *gin.Context) common.Result {
	var order models.XycOrder
	err := c.ShouldBindJSON(&order)
	if err != nil {
		return common.R.Fail(err.Error())
	}
	err = OrderDao.AddOrderDao(order)
	if err != nil {
		return common.R.Fail(err.Error())
	}
	return common.R.Success("添加成功")
}

// 根据userId查询订单列表
// @Summary 获取订单列表
// @description 根据userId查询订单列表
// @Tags xyc_shop前台接口
// @Accept json
// @Produce json
// @Param userId query int true "用户id"
// @Success 200 {object} common.RT[[]models.XycOrder]
// @Router /front/xyc/order/findOrderByUserId [get]
func findOrderByUserId(c *gin.Context) common.Result {
	userIdStr := c.Query("userId")
	userId, _ := strconv.Atoi(userIdStr)
	orders, err := OrderDao.FindOrderByUserIdDao(userId)
	if err != nil {
		return common.R.Fail(err.Error())
	}
	return common.R.Success(orders)
}
